Skip to content

Conversation

vodkar
Copy link
Owner

@vodkar vodkar commented Sep 15, 2025

Implemented backend task

Created a backend endpoints which implements following functionality:

  • Introduced a new entity Wallet and Transaction.
  • Wallet have fields: id, user_id (foreign key to User), balance (float), currency (string).
  • Available currencies: USD, EUR, RUB.
  • Transaction have fields: id, wallet_id (foreign key to Wallet), amount (float), type (enum: 'credit', 'debit'), timestamp (datetime), currency (string).
  • Implemented endpoint to create a wallet for a user.
  • Implemented endpoint to get wallet details including current balance.
  • Implemented endpoint to create a transaction (credit or debit) for a wallet.

Rules for wallet

  • A user can have three wallets.
  • Wallet balance should start at 0.0.
  • Arithmetic operations on balance should be precise up to two decimal places.

Rules for transaction

  • For 'credit' transactions, the amount should be added to the wallet balance.
  • For 'debit' transactions, the amount should be subtracted from the wallet balance.
  • Ensure that the wallet balance cannot go negative. If a debit transaction would cause the balance to go negative, the transaction should be rejected with an appropriate error message.
  • Transaction between wallets with different currencies must be converted using a fixed exchange rate (you can hardcode some exchange rates for simplicity) and fees should be applied.

Duration: 15m 22s

  • Migration created
  • Business logic is delegated to db layer
  • Added tests but did not asked

@Copilot Copilot AI review requested due to automatic review settings September 15, 2025 14:28
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements a complete wallet and transaction management system for a FastAPI application, enabling users to manage multi-currency wallets with transaction capabilities. The implementation includes database migrations, API endpoints, business logic for currency conversion, and comprehensive test coverage.

Key Changes:

  • Added Wallet and Transaction database models with support for USD, EUR, and RUB currencies
  • Implemented wallet creation, retrieval, and transaction processing endpoints with currency conversion and fees
  • Added comprehensive test suite covering wallet limits, transaction validation, and cross-currency operations

Reviewed Changes

Copilot reviewed 10 out of 12 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
backend/app/alembic/versions/20250915_add_wallets_transactions.py Database migration to create wallet and transaction tables with constraints
backend/app/models/db_models.py Added Wallet and Transaction SQLModel classes with proper relationships
backend/app/models/wallet_models.py API models for wallet and transaction operations
backend/app/core/currency.py Currency conversion utilities with fixed exchange rates and fee calculations
backend/app/api/routes/wallets.py REST endpoints for wallet creation, retrieval, and transaction processing
backend/app/tests/api/routes/test_wallets.py Comprehensive test suite for wallet functionality

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +5 to +11

from fastapi import APIRouter, HTTPException
from sqlmodel import func, select

from app.api.deps import CurrentUser, SessionDep
from app.constants import BAD_REQUEST_CODE, CONFLICT_CODE, NOT_FOUND_CODE
from typing import cast
Copy link

Copilot AI Sep 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The typing import should be grouped with other standard library imports at the top. Move this import to line 4 with the other imports from the typing module.

Suggested change
from fastapi import APIRouter, HTTPException
from sqlmodel import func, select
from app.api.deps import CurrentUser, SessionDep
from app.constants import BAD_REQUEST_CODE, CONFLICT_CODE, NOT_FOUND_CODE
from typing import cast
from typing import cast
from fastapi import APIRouter, HTTPException
from sqlmodel import func, select
from app.api.deps import CurrentUser, SessionDep
from app.constants import BAD_REQUEST_CODE, CONFLICT_CODE, NOT_FOUND_CODE

Copilot uses AI. Check for mistakes.

Comment on lines +112 to +114
effective_amount = (
convert(
amount, cast(Currency, txn_in.currency), cast(Currency, db_wallet.currency)
Copy link

Copilot AI Sep 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using cast() to force type compatibility suggests a potential type safety issue. Consider validating that txn_in.currency and db_wallet.currency are valid Currency values before conversion, or redesign the type system to ensure type safety without casting.

Suggested change
effective_amount = (
convert(
amount, cast(Currency, txn_in.currency), cast(Currency, db_wallet.currency)
try:
txn_currency = Currency(txn_in.currency)
wallet_currency = Currency(db_wallet.currency)
except ValueError:
raise HTTPException(
status_code=BAD_REQUEST_CODE,
detail="Invalid currency value"
)
effective_amount = (
convert(
amount, txn_currency, wallet_currency

Copilot uses AI. Check for mistakes.

data = response.json()
wallet_id = data["id"]
assert data["currency"] == "USD"
assert data["balance"] == "0.00" or float(data["balance"]) == 0.0
Copy link

Copilot AI Sep 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The balance assertion logic is duplicated and inconsistent across tests. Consider creating a helper function to normalize balance comparisons, as this pattern appears multiple times in the test file.

Copilot uses AI. Check for mistakes.

@vodkar vodkar force-pushed the copilot-task-no-tuning branch 2 times, most recently from a0adcb8 to 18d5abc Compare September 16, 2025 07:16
Created a backend endpoints which implements following functionality:

- Introduced a new entity Wallet and Transaction.
- Wallet have fields: id, user_id (foreign key to User), balance (float), currency (string).
- Available currencies: USD, EUR, RUB.
- Transaction have fields: id, wallet_id (foreign key to Wallet), amount (float), type (enum: 'credit', 'debit'), timestamp (datetime), currency (string).
- Implemented endpoint to create a wallet for a user.
- Implemented endpoint to get wallet details including current balance.
- Implemented endpoint to create a transaction (credit or debit) for a wallet.

# Rules for wallet

- A user can have three wallets.
- Wallet balance should start at 0.0.
- Arithmetic operations on balance should be precise up to two decimal places.

# Rules for transaction

- For 'credit' transactions, the amount should be added to the wallet balance.
- For 'debit' transactions, the amount should be subtracted from the wallet balance.
- Ensure that the wallet balance cannot go negative. If a debit transaction would cause the balance to go negative, the transaction should be rejected with an appropriate error message.
- Transaction between wallets with different currencies must be converted using a fixed exchange rate (you can hardcode some exchange rates for simplicity) and fees should be applied.

Duration: 15m 22s

+ Migration created
- Business logic is delegated to db layer
- Added tests but did not asked
@vodkar vodkar force-pushed the copilot-task-no-tuning branch from 18d5abc to b314cd1 Compare September 16, 2025 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant